import { AlertController } from '@ionic/angular';
import { forEach } from '@angular/router/src/utils/collection';
import { ModalOfEditMoreInfoComponent } from './../modal-of-edit-more-info/modal-of-edit-more-info.component';
import { Component, OnInit, Input } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { ModalOfPersonSearchComponent } from '../modal-of-person-search/modal-of-person-search.component';

@Component({
  selector: 'app-modal-of-local-edit',
  templateUrl: './modal-of-local-edit.component.html',
  styleUrls: ['./modal-of-local-edit.component.scss'],
})
export class ModalOfLocalEditComponent implements OnInit {
  @Input() tagData: any;
  newHtmlObj: any;
  constructor(
    private modalCtrl: ModalController,
    public alertController: AlertController,
    private appGlobalService: AppGlobalService
  ) { }

  ngOnInit() {
    this.newHtmlObj = JSON.stringify(this.tagData.subHtmlObj);
  }


  cancel(){
    this.modalCtrl.dismiss();
  }

  async del(info, i) {
    let message =  '确定删除？';
    if (this.tagData.dataType === 'houseType') {
      message = '确定删除该户型吗？';
    } else if (this.tagData.dataType === 'preSale') {
      message = '确定删除该预售证吗？';
    }
    const alert = await this.alertController.create({
      header: '',
      subHeader: '',
      message: '确定删除？',
      buttons: [{
        text: '取消',
        role: 'cancel',
        cssClass: 'secondary',
        handler: () => {
          console.log('取消');
        }
      }, {
        text: '删除',
        cssClass: 'LJ',
        handler: () => {
          this.tagData.dataValue.splice(i, 1);
        }
      }, ]
    });
    await alert.present();
  }

  async modify(info, i) {
    const that = this;
    this.tagData.subHtmlObj.forEach(element => {
      element.value = info[element.formKey];
    });

    const modal = await this.modalCtrl.create({
      component: ModalOfEditMoreInfoComponent,
      componentProps: { value: this.tagData , newAdd: false, editItem: info} });
    modal.onDidDismiss().then(function(data) {

      if (data.data && data.data.form) {
        that.tagData.dataValue.splice(i, 1, data.data.form.value);
      } 
    });
    return await modal.present();
  }
 
  async add() {
      this.tagData.subHtmlObj = JSON.parse(this.newHtmlObj);
      const that = this;
      const modal = await this.modalCtrl.create({
      component: ModalOfEditMoreInfoComponent,
      componentProps: { value: this.tagData , newAdd: true} });
      modal.onDidDismiss().then(function(data) {
        if (data.data && data.data.form) {
          if (that.tagData.dataValue) {
            that.tagData.dataValue.push(data.data.form.value);  
          } else {
            that.tagData.dataValue = [];
            that.tagData.dataValue.push(data.data.form.value);
          }
        } else {

        }
    });
      return await modal.present();
    }
}
